
package com.android.support.jhf.cropimage.cropper;

import com.android.support.jhf.R;
import com.android.support.jhf.cropimage.cropper.CropImageView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
/**
 * 用于测试CropImageView
 * @author lenovo
 *
 */
public class CropperActivity extends Activity {

    // Static final constants
    private static final int DEFAULT_ASPECT_RATIO_VALUES = 10;
    private static final int ROTATE_NINETY_DEGREES = 90;
    private static final String ASPECT_RATIO_X = "ASPECT_RATIO_X";
    private static final String ASPECT_RATIO_Y = "ASPECT_RATIO_Y";
    private static final int ON_TOUCH = 1;

    // Instance variables
    private int mAspectRatioX = DEFAULT_ASPECT_RATIO_VALUES;
    private int mAspectRatioY = DEFAULT_ASPECT_RATIO_VALUES;

    private Bitmap croppedImage;
    private String mOutPathString;
    private String mInPathString;

    // Saves the state upon rotating the screen/restarting the activity
    @Override
    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt(ASPECT_RATIO_X, mAspectRatioX);
        bundle.putInt(ASPECT_RATIO_Y, mAspectRatioY);
    }

    // Restores the state upon rotating the screen/restarting the activity
    @Override
    protected void onRestoreInstanceState(Bundle bundle) {
        super.onRestoreInstanceState(bundle);
        mAspectRatioX = bundle.getInt(ASPECT_RATIO_X);
        mAspectRatioY = bundle.getInt(ASPECT_RATIO_Y);
    }

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.android_support_jhf_activity_cropper);
        
        mOutPathString = getIntent().getStringExtra("outpath");
        mInPathString = getIntent().getStringExtra("inpath");

        // Initialize components of the app
        final CropImageView cropImageView = (CropImageView) findViewById(R.id.cropImageView);
//        final SeekBar aspectRatioXSeek = (SeekBar) findViewById(R.id.aspectRatioXSeek);
//        final SeekBar aspectRatioYSeek = (SeekBar) findViewById(R.id.aspectRatioYSeek);
//        final ToggleButton fixedAspectRatioToggle = (ToggleButton) findViewById(R.id.fixedAspectRatioToggle);
//        Spinner showGuidelinesSpin = (Spinner) findViewById(R.id.showGuidelinesSpin);
        
//        //Sets the rotate button
//        final Button rotateButton = (Button) findViewById(R.id.Button_rotate);
//        rotateButton.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                cropImageView.rotateImage(ROTATE_NINETY_DEGREES);
//            }
//        });
        
        // Sets initial aspect ratio to 10/10, for demonstration purposes
        cropImageView.setAspectRatio(DEFAULT_ASPECT_RATIO_VALUES, DEFAULT_ASPECT_RATIO_VALUES);

        // Sets aspectRatioX
//        final TextView aspectRatioX = (TextView) findViewById(R.id.aspectRatioX);
//
//        aspectRatioXSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar aspectRatioXSeek, int progress, boolean fromUser) {
//                try {
//                    mAspectRatioX = progress;
//                    cropImageView.setAspectRatio(progress, mAspectRatioY);
//                    aspectRatioX.setText(" " + progress);
//                } catch (IllegalArgumentException e) {
//                }
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//            }
//        });
//
//        // Sets aspectRatioY
//        final TextView aspectRatioY = (TextView) findViewById(R.id.aspectRatioY);
//
//        aspectRatioYSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar aspectRatioYSeek, int progress, boolean fromUser) {
//                try {
//                    mAspectRatioY = progress;
//                    cropImageView.setAspectRatio(mAspectRatioX, progress);
//                    aspectRatioY.setText(" " + progress);
//                } catch (IllegalArgumentException e) {
//                }
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//            }
//        });


        // Sets up the Spinner
//        showGuidelinesSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                cropImageView.setGuidelines(i);
//            }
//
//            public void onNothingSelected(AdapterView<?> adapterView) {
//                return;
//            }
//        });

        final Button cropButton = (Button) findViewById(R.id.okButton);
        cropButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                croppedImage = cropImageView.getCroppedImage();
            }
        });
        
        final Button cancelButton = (Button)findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				finish();
			}
		});

    }

}
